<template>
  <el-dialog
    title="新增标签"
    :visible.sync="addDialogVisible"
    width="30%"
    @close="$emit('close')"
  >
    <el-form :model="tags" :rules="rules" ref="tagsRef" label-width="80px">
      <el-form-item
        :label="$t('所属学科')"
        v-if="$route.params.id ? false : true"
      >
        <el-select v-model="tags.subjectID" placeholder="请选择" clearable>
          <el-option
            v-for="item in list"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('目录名称')" prop="tagName">
        <el-input
          v-model.trim="tags.tagName"
          placeholder="请输入目录名称"
          @keyup.enter.native="addTag"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="$emit('close')">{{ $t("table.cancel") }}</el-button>
      <el-button type="primary" @click="addTag">{{
        $t("table.confirm")
      }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { add } from '@/api/hmmm/tags'

export default {
  name: 'TagsAddIndex',
  props: {
    subjectAdd: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      // 控制添加弹层的显示与隐藏
      addDialogVisible: true,
      tags: {
        subjectID: null,
        tagName: ''
      },
      // 简单学科列表信息
      list: [],
      // 新增标签验证规则
      rules: {
        tagName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
      }
    }
  },
  created () {
    if (this.subjectAdd.subjectID === undefined) {
      this.getSubjectList()
    }
  },
  methods: {
    // 获取简单学科列表
    getSubjectList () {
      simple()
        .then(({ data }) => {
          this.list = data
        })
        .catch((e) => {
          this.$message('获取学科列表失败')
        })
    },
    // 点击确定按钮添加目录
    addTag () {
      this.$refs.tagsRef.validate((valid) => {
        if (!valid) return false
        add({
          subjectID: this.tags.subjectID ? this.tags.subjectID : this.subjectAdd.subjectID,
          tagName: this.tags.tagName
        })
          .then((data) => {
            this.$emit('addTags')
            this.$message.success('添加学科标签成功')
          })
          .catch((e) => {
            this.$emit('close')
            this.$message('添加失败，请稍后重试')
          })
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
